import axios from 'axios'

import {
	Message
} from 'element-ui'

const service = axios.create({
	baseURL: 'http://localhost:9529',
	timeout: 5000
})

service.interceptors.request.use(config => {
	// Do something before request is sent
	console.log('config', config);
	return config
}, error => {
	// Do something with request error
	console.log(error)
	// 跳转error页面
	router.push({
		path: "/error"
	});
	return Promise.reject(error)
})

service.interceptors.response.use(response => {
	// Do something with response data	
	/**
	 * code为非 20000 是抛错 可结合自己业务进行修改
	 */
	const res = response.data
	if (res.code !== 20000) {
		Message({
			message: res.message,
			type: 'error',
			duration: 5 * 1000
		})
		router.push({
			path: "/error"
		});
	} else {
		return response.data
	}
}, error => {
	// Do something with response error
	console.log('err', error)
	Message({
		message: error.message,
		type: 'error',
		duration: 5 * 1000
	})
	// 跳转error页面
	router.push({
		path: "/error"
	});
	return Promise.reject(error)
})

// 导出 axios 实例
export default service
